package com.ytjj.qmyx.supplychain.api.controller;

import com.github.pagehelper.PageHelper;
import com.ytjj.qmyx.supplychain.common.api.CommonPage;
import com.ytjj.qmyx.supplychain.common.api.CommonResult;
import com.ytjj.qmyx.supplychain.common.model.request.BatchProductLabelRequest;
import com.ytjj.qmyx.supplychain.mapper.ProductLabelMapper;
import com.ytjj.qmyx.supplychain.mapper.entity.YxProductLabel;
import com.ytjj.qmyx.supplychain.mapper.entity.YxProductLabelProduct;
import com.ytjj.qmyx.supplychain.service.ProductLabelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

/**
 * 标签管理
 */
@RestController
@RequestMapping("/label")
public class LabelController {
    @Resource
    private ProductLabelMapper productLabelMapper;
    @Autowired
    private ProductLabelService productLabelService;

    /**
     * 供应链商品标签列表
     * @param productLabelName
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/productLabelList")
    public CommonPage queryProductLabelList(@RequestParam(value = "productLabelName", required = false) String productLabelName,
                                               @RequestParam(value = "pageNum", defaultValue = "1", required = false)Integer pageNum,
                                               @RequestParam(value = "pageSize", defaultValue = "10", required = false)Integer pageSize) {
        Example example = new Example(YxProductLabel.class);
        Example.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(productLabelName)) {
            criteria.andLike("productLabelName", "%" + productLabelName + "%");
        }
        PageHelper.startPage(pageNum, pageSize);
        List<YxProductLabel> productLabels = productLabelMapper.selectByExample(example);
        productLabels.stream().forEach(item ->{
            item.setUseNum(Math.toIntExact(productLabelService.userNumBylabelId(item.getProductLabelId())));
        });
        CommonPage<YxProductLabel> page = CommonPage.restPage(productLabels);
        return page;
    }

    /**
     * 增加修改标签
     * @param productLabelName 名
     * @param productLabelId id
     * @return CommonResult
     */
    @GetMapping("/addAndUpdate")
    public CommonResult addAndUpdate(@RequestParam(value = "productLabelName") String productLabelName,
                                     @RequestParam(value = "productLabelId",required = false) Integer productLabelId,
                                     @RequestParam(value = "colour") String colour,
                                     @RequestParam(value = "adminId") Integer adminId) {
        return productLabelService.addAndUpdate(productLabelName,productLabelId,adminId,colour);
    }

    /**
     *  删除标签
     * @param productLabelId 标签id
     * @return CommonResult
     */
    @GetMapping("/delete")
    public CommonResult delete(@RequestParam(value = "productLabelId") Integer productLabelId) {
        return productLabelService.delete(productLabelId);
    }

    /**
     * 查询所有标签
     * @return
     */
    @GetMapping("/selectAllLabel")
    public List<YxProductLabel> selectAllLabel(){
        return productLabelService.selectAllLabel();
    }

    /**
     * 批量添加商品标签
     */
    @PostMapping("/batchAddProductLabel")
    public CommonResult batchAddProductLabel(@RequestBody BatchProductLabelRequest request){
        return productLabelService.batchAddProductLabel(request);
    }

    /**
     * 批量添加商品标签
     */
    @PostMapping("/batchDeleteProductLabel")
    public CommonResult batchDeleteProductLabel(@RequestBody BatchProductLabelRequest request){
        return productLabelService.batchDeleteProductLabel(request);
    }

}
